//OrderServlet.java
package com.ylaihui.web;

import com.ylaihui.pojo.Cart;
import com.ylaihui.pojo.Order;
import com.ylaihui.pojo.OrderItem;
import com.ylaihui.pojo.User;
import com.ylaihui.service.OrderService;
import com.ylaihui.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lisimmy www.ylaihui.com
 * @create 2021-08-03
 */
public class OrderServlet extends BaseServlet {

    OrderService orderService = new OrderServiceImpl();

    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //获取登录用户
        User user = (User) req.getSession().getAttribute("userObj");
        //如果用户未登录，强制用户登录
        if (user == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }

        Integer id = user.getId();

        String orderId = orderService.createOrder(cart, id);


        req.getSession().setAttribute("orderId", orderId);
        //req.setAttribute("orderId",orderId);
        //会有表单重复提交的问题
        //req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");

    }

    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Order> orders = orderService.showAllOrders();

        req.setAttribute("orders", orders);
        // 请求转发
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req, resp);
    }
    protected void showUserOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        User userObj = (User) req.getSession().getAttribute("userObj");
        List<Order> orders = orderService.showUserOrders(userObj.getId());

        req.setAttribute("orders", orders);
        // 请求转发
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req, resp);
    }

    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取 orderId
        String orderId = req.getParameter("orderId");
        //2.修改 订单表 by id
        orderService.sendOrder(orderId);

        resp.sendRedirect(req.getContextPath() + "/manager/orderServlet?action=showAllOrders");
    }

    protected void showOrderDetails(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取 orderId
        String orderId = req.getParameter("orderId");
        //2.根据id,获取 item详情，并存入请求域
        List<OrderItem> orderItems = orderService.showOrderDetails(orderId);
        req.setAttribute("orderItems", orderItems);

        // 获取订单的信息，并存入请求域
        Order order = orderService.queryOrderByOrderId(orderId);
        req.setAttribute("order",order);

        //3.请求转发到订单详情页面
        req.getRequestDispatcher("/pages/manager/order_details.jsp").forward(req,resp);
    }


}
